package com.mail.anteater.dao;

import java.util.ArrayList;
import java.util.Date;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;

import com.mail.anteater.hibernate.HibernateUtil;
import com.mail.anteater.hibernate.mapping.TempInboxMapper;
import com.mail.anteater.hibernate.mapping.TempOutboxMapper;

public class MapperDAO {

	public static TempInboxMapper isTempInboxAvailable(String userId, Date lastInboxAt, String filter, boolean isNestedOperation){
		DetachedCriteria cri = DetachedCriteria.forClass(TempInboxMapper.class);
		cri.add(Restrictions.eq("userId", userId));
		cri.add(Restrictions.eq("lastInboxAt", lastInboxAt));
		cri.add(Restrictions.eq("filter", filter));
		
		ArrayList<Object> list = HibernateUtil.list(cri, isNestedOperation);
		if(list != null){
			if(list.size() > 0){
				return (TempInboxMapper) list.get(0);
			}
		}
		return null;
	}
	
	public static TempOutboxMapper isTempOutboxAvailable(String userId, Date lastOutboxAt, String filter, boolean isNestedOperation){
		DetachedCriteria cri = DetachedCriteria.forClass(TempOutboxMapper.class);
		cri.add(Restrictions.eq("userId", userId));
		cri.add(Restrictions.eq("lastOutboxAt", lastOutboxAt));
		cri.add(Restrictions.eq("filter", filter));
		
		ArrayList<Object> list = HibernateUtil.list(cri, isNestedOperation);
		if(list != null){
			if(list.size() > 0){
				return (TempOutboxMapper) list.get(0);
			}
		}
		return null;
	}
}
